package org.jumiz.dbview.own.service;

import com.alibaba.fastjson2.JSON;
import org.jumiz.dbview.own.entity.ConnectEntity;
import org.jumiz.dbview.own.vo.SubMenuVo;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.sql.*;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class H2DatabasesServiceImpl implements DatabasesService {

    @Override
    public Connection getConnection(ConnectEntity entity) throws Exception {
        Class.forName("org.h2.Driver");
        return DriverManager.getConnection(entity.getAddress(), entity.getLoginName(), entity.getPassword());
    }

    @Override
    public List<String> getColumns(ConnectEntity entity, String schemaName, String tableName) {
        try (Connection connection = getConnection(entity);) {
            String sql = "SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT " +
                    "FROM INFORMATION_SCHEMA.COLUMNS " +
                    "WHERE TABLE_NAME = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, tableName);
            ResultSet resultSet = statement.executeQuery();
            ArrayList<String> columns = new ArrayList<>();
            while (resultSet.next()) {
                String columnName = resultSet.getString("COLUMN_NAME");
                columns.add(columnName);
            }
            return columns;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getTableData(ConnectEntity entity, String tableName, List<String> columns) {
        String sql = "SELECT * FROM \"" + tableName + "\";";
        System.out.println(sql);
        try (Connection connection = getConnection(entity);) {
            PreparedStatement statement = connection.prepareStatement(sql);
            ResultSet resultSet = statement.executeQuery();
            ArrayList<Map<String, Object>> result = new ArrayList<>();
            while (resultSet.next()) {
                Map<String, Object> rowMap = new HashMap<>();
                for (String column : columns) {
                    rowMap.put(column, resultSet.getObject(column));
                }
                result.add(rowMap);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Mono<Boolean> testConnection(ConnectEntity entity) {
        try {
            getConnection(entity);
            return Mono.just(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Mono.just(false);
    }

    @Override
    public List<SubMenuVo> getMenusData(ConnectEntity entity) {
        try (Connection connection = getConnection(entity)) {
            PreparedStatement statement = connection.prepareStatement("show tables;");
            ResultSet resultSet = statement.executeQuery();
            ArrayList<String> tables = new ArrayList<>();
            while (resultSet.next()) {
                String name = resultSet.getString(1);
                System.out.println(name);
                tables.add(name);
            }
            SubMenuVo subMenuVo = new SubMenuVo();
            subMenuVo.setLabel("表");
            subMenuVo.setType("table");
            subMenuVo.setData(tables);
            return Arrays.asList(subMenuVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
